import { ElMessage } from 'element-plus'
import router from '../router/index';
import axios from 'axios';
/**
 * 创建axios实例
 */
const ENV = process.env.NODE_ENV;
const host = ENV === 'development' ? 'http://127.0.0.1:3800' : 'https://www.baidu.com';
const service = axios.create({
    baseURL: host,
    timeout: 3000,
});
/**
 * 请求拦截器
 */
service.interceptors.request.use((config) => {
    // 在发送请求之前做些什么
    if (config.url.indexOf('login') < 0 && config.url.indexOf('register') < 0) {
        config.headers.Authorization = localStorage.getItem('token') || '';
        // config.headers.token = localStorage.getItem('token') || '';
    }
    return config;
}, error => {
    // 对请求错误做些什么
    return Promise.reject(error);
});
/**
 * 响应拦截器
 */
service.interceptors.response.use((response) => {
     console.log(response,'----------响应拦截器');
    // 对响应数据做点什么
    const { code, data, msg } = response.data;
    console.log(response.data,'响应拦截器',data,code);
    if (code === 0) {
        return response.data;
    }else{
        ElMessage({
            message: msg,
            type: 'error'
        });
    }
    if (msg == '身份认证失败') {
        router.push('/login');
    }
});
/**
 * 封装请求
 */
const request = (options) => {
    if (options.method == 'get') {
        options.params = options.data;
    }
    console.log(options,'封装请求');
    return service(options);
};
export default request;